<?php

/**
* alltosun.com 分类静态方法 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-8-8 下午12:36:18 $
* $Id$
*/

class category_helper
{
    /**
     *通过父级id 和 res_name查询分类列表
     * @param int $parent_id
     * @return array()
     */
    public static function get_category_list_by_parent_id($parent_id, $res_name = '')
    {
        $filter['parent_id'] = (int)$parent_id;
        if ($res_name) {
        $filter['res_name'] = $res_name;
        }
        $filter['status']   = 1;
        return _model('category')->getList($filter, " ORDER BY `view_order` ASC,`res_name` DESC,concat(`path`,'-',`id`) ASC");
    }

    /**
     *通过res_name  查询分类列表
     * @return array()
     */
    public static function get_list_by_res_name($res_name = 'goods')
    {
        $filter['res_name'] = $res_name;
        $filter['status']   = 1;

        $order = ' ORDER BY `res_name` DESC,concat(`path`,'-',`id`) ASC';
        if ($res_name == 'article') {
            $order = ' ORDER BY `view_order` ASC ';
        }

        return _model('category')->getList($filter, $order);
    }

    /**
     *通过父级id 和 查询所有子级id
     * @param int $parent_id
     * @return array()
     */
    public static function get_children_ids($parent_id)
    {
        $category_info = _uri('category', $parent_id);
        $path = $category_info['path'].'-'.$category_info['id'];

        $filter['`path` LIKE '] = $path.'%';

        $category_ids = _model('category')->getFields('id', $filter);

        return $category_ids;
    }

    /**
     *通过分类id 获取分类路径
     * @param int $id
     * @return array()
     */
    public static function get_path_by_id($id)
    {
        $category_info = _uri('category', (int)$id);
        $path = $category_info['path'].'-'.$category_info['id'];
        return explode('-', $path);
    }

    /**
     *通过分类id 获取顶级分类id
     * @return int
     */
    public static function get_top_id($category_id)
    {
        if (!$category_id) {
            return 0;
        }

        $category_info = _uri('category', $category_id);
        if ($category_info['parent_id'] == 0) {
            return $category_id;
        }

        $path_arr = explode('-', $category_info['path']);

        return $path_arr[1];
    }

    public static function get_category_title($category_id)
    {
        if (!$category_id) {
            return '';
        }

        return _uri('category', $category_id, 'title');
    }

    public static function get_all_ids($category_id)
    {
        if (!$category_id) {
            return array();
        }

        $category_path = _uri('category', $category_id, 'path');

        $category_ids = explode('-', $category_path);
        $category_ids[] = $category_id;

        return $category_ids;
    }
}
?>